package com.example.mediacenterufrj;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;

public class Resources {
	public static class ThumbnailInfo {
		
		public ThumbnailInfo(final String filepath) {
			this.videofilePath = filepath;
			this.thumbnail = ThumbnailUtils.createVideoThumbnail(filepath, MediaStore.Images.Thumbnails.MINI_KIND);		
		}
		
		public Bitmap thumbnail;
		public String videofilePath = "";
	}
	
	public static class ImageInfo {
		
		public ImageInfo(final String filepath) {
			this.imagePath = filepath;
			this.image = BitmapFactory.decodeFile(filepath);		
		}
		
		public Bitmap image;
		public String imagePath = "";
	}
	
	public static String shareText = "";
	public static ArrayList<ThumbnailInfo> listaVideoThumbnails = new ArrayList<ThumbnailInfo>();
	public static ArrayList<ImageInfo> listaImagens = new ArrayList<ImageInfo>();
	public static Bitmap wallpaper;
	
	public static void loadShareText(String sharedTextFile) {
		File shareFile = new File(sharedTextFile);
		
		if (shareFile.exists()) {

		   try {
		       FileReader reader = new FileReader(shareFile);
		       char[] chars = new char[(int) shareFile.length()];
		       reader.read(chars);
		       Resources.shareText = new String(chars);
		       reader.close();
		   } catch (IOException e) {
		       //e.printStackTrace();
		   }
	
		}		
	}	
	
	public static void loadWallpaper(String wallpaperPath) {
		try{
		Resources.wallpaper = BitmapFactory.decodeFile(wallpaperPath);
		} catch (Exception e) {}
	}
	
	public static void populateVideos(String videoFileContainerPath) {
		
		File dir = new File(videoFileContainerPath);
		
		if (dir.exists())  {
			File[] files = dir.listFiles();
			
			for (final File videoFile : files) { 
				if (!videoFile.getAbsolutePath().contains(".mp4")) continue;
				
				new Thread(new Runnable() {
					@Override
					public void run() {
						Resources.listaVideoThumbnails.add(new ThumbnailInfo(videoFile.getAbsolutePath()));
					}
				}).run();
				
			}
			
		}
		
		
	}
	
	public static void populateImages(String imageFileContainerPath) {
		
		File dir = new File(imageFileContainerPath);
		
		if (dir.exists())  {
			File[] files = dir.listFiles();
			
			for (final File imageFile : files) { 
				if (!(imageFile.getAbsolutePath().contains(".jpg") ||
					  imageFile.getAbsolutePath().contains(".png"))) continue;
				
				new Thread(new Runnable() {
					@Override
					public void run() {
						Resources.listaImagens.add(new ImageInfo(imageFile.getAbsolutePath()));
					}
				}).run();
				
			}
			
		}
		
		
	}

}
